package com.zhl.media.app.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.di.component.AppComponent;
import com.muddzdev.styleabletoast.StyleableToast;
import com.zhl.media.app.R;
import com.zhl.media.app.di.component.DaggerMineComponent;
import com.zhl.media.app.di.module.MineModule;
import com.zhl.media.app.model.AdJust;
import com.zhl.media.app.mvp.contract.MineContract;
import com.zhl.media.app.mvp.presenter.MinePresenter;
import com.zhl.media.app.mvp.ui.activity.MediaActivity;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.commonres.base.AppFragment;
import com.zhl.media.commonres.view.BaseItemView;
import com.zhl.media.commonsdk.core.EventBusCode;
import com.zhl.media.commonsdk.core.RouterHub;
import com.zhl.media.commonsdk.data.UserUtil;
import com.zhl.media.commonsdk.utils.Utils;

import org.simple.eventbus.Subscriber;

import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class MineFragment extends AppFragment<MinePresenter> implements MineContract.View {


    @BindView(R.id.seting)
    BaseItemView seting;
    @BindView(R.id.workProd)
    BaseItemView workProd;
    @BindView(R.id.tz)
    BaseItemView tz;

    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.org)
    TextView org;

    @BindView(R.id.user_info)
    LinearLayout linearLayout;
    public static MineFragment newInstance() {
        Bundle args = new Bundle();
        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerMineComponent.builder().appComponent(appComponent).mineModule(new MineModule(this)).build().inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mine_fragment, container, false);
        return view;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        loadTags();
        loadTz();
        seting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(RouterHub.APP_SETINGACTIVITY).navigation(getActivity());
            }
        });
        workProd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.navigation(getActivity(),RouterHub.APP_WORKPRODACTIVITY);
            }
        });
        mPresenter.getUserInfo();
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.navigation(getActivity(),RouterHub.APP_USERINFOACTIVITY);
            }
        });
        tz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(UserUtil.getAdjust()){
                    mPresenter.saveAbnormalInfo();
                }else {
                    StyleableToast.makeText(getActivity(), "暂无待调整数据需要上传", Toast.LENGTH_SHORT, R.style.mytoast).show();
                }
            }
        });

    }

    // 更新待上传数
    @Subscriber(tag = "update_dsc_num")
    public void update_dsc_num(int code) {
        if(code == EventBusCode.DSCNUM){
            loadTags();
            loadTz();
        }
    }

    private void loadTz(){
        if(UserUtil.getAdjust()){
            tz.setRightBg();
            tz.setRightText("您有调整结果需要上传");
        }else {
            tz.setRightBgGONE();
            tz.setRightText("");
        }
    }

    private void loadTags() {
        if(EntityManager.getInstance(getActivity()).getMediaPhotoDao().loadAll().size()>0){
            workProd.setRightBg();
        }else {
            workProd.setRightBgGONE();
        }
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {
        showDiglog();
    }

    @Override
    public void hideLoading() {
        hideDiglog();
    }

    @Override
    public void showMessage(@NonNull String message) {

    }

    @Override
    public void launchActivity(@NonNull Intent intent) {

    }

    @Override
    public void killMyself() {

    }

    @Override
    public void onSuccess(Map map) {
        userName.setText((String)map.get("fullName"));
        org.setText((String)map.get("deptName"));
    }
}
